package com.digitalart.site.commons {
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.events.EventDispatcher;	

	/**
	 * @author johan.chaves
	 */
	public class CustomXmlLoader extends EventDispatcher {
		
		private static var _instance : CustomXmlLoader;
		
		public static const XML_LOAD_COMPLETE : String = "xmlLoadComplete";
		
		
		/**********************************Constructor*************************/
		
		public function CustomXmlLoader(singleton : SingletonEnforcer) {
			
		}
		
		
		/**********************************************************************/
		
		/**********************************Methods*************************/
		
		public function loadXml(path : String):void{
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, completeLoading);
			loader.addEventListener(IOErrorEvent.IO_ERROR, failXmlLoading);
			var request:URLRequest = new URLRequest(path);
			loader.load(request);	
		}

		private function failXmlLoading(event : IOErrorEvent) : void {
			trace("XML Load failed");
		}

		private function completeLoading(e:Event):void {
			e.target.removeEventListener(Event.COMPLETE, completeLoading);
			var xmlFile : XML;
			var loader:URLLoader = e.target as URLLoader;
			xmlFile = new XML(loader.data);			
			xmlFile.ignoreWhite = true;							
			dispatchEvent(new CustomEvent(XML_LOAD_COMPLETE,xmlFile));
		}
		
		
		/**********************************************************************/
		/**************************Getters & Setters***************************/
					
		public static function getInstance(): CustomXmlLoader{
			if(_instance == null){
				_instance = new CustomXmlLoader(new SingletonEnforcer());
			}
			return _instance;
		}
	}
}
class SingletonEnforcer{}